package cn.edu.gzhu.workOrderManagement.controller;

import cn.edu.gzhu.workOrderManagement.constants.MessageConstant;
import cn.edu.gzhu.workOrderManagement.pojo.dto.workOrder.WorkOrderCreateDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.workOrder.WorkOrderListDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.workOrder.WorkOrderRepeatDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.workOrder.WorkOrderUpdateDto;
import cn.edu.gzhu.workOrderManagement.pojo.vo.WorkOrderVo;
import cn.edu.gzhu.workOrderManagement.result.Result;
import cn.edu.gzhu.workOrderManagement.service.WorkOrderService;
import cn.edu.gzhu.workOrderManagement.util.WorkOrderUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/workOrder")
@Tag(name = "工单管理接口")
public class WorkOrderController {
    @Autowired
    WorkOrderService workOrderService;

    /**
     * 创建工单接口，前端解析excel数据然后传递给后端，或者用户直接填写表单
     * @param token
     * @param workOrderCreateDtos
     * @return
     */
    //这个方法手写校验，以便批量创建工单时，找到问题工单
    @Operation(summary = "创建工单")
    @PostMapping("/create")
    public Result createWorkOrder(@RequestHeader String token, @RequestBody  List<WorkOrderCreateDto> workOrderCreateDtos) {
        List<Integer> illegalSubscript = WorkOrderUtil.validateWorkOrderFields(workOrderCreateDtos);

        if (!illegalSubscript.isEmpty()) {
            return Result.error(illegalSubscript.toString());//返回有问题的工单的下标，从0开始
        }

        Boolean isSuccess = workOrderService.createWorkOrder(workOrderCreateDtos);
        if (!isSuccess) {
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }

    /**
     * 按照条件分页查询工单
     *
     * @param token
     * @param workOrderListDto
     * @return
     */
    @Operation(summary = "工单分页查询")
    @GetMapping("/list")
    public Result<IPage<WorkOrderVo>> listWorkOrderByConditions(@RequestHeader String token,@Validated WorkOrderListDto workOrderListDto) {
        //分页参数不能为空，起始页数从1开始，页面长度不超过100
        IPage<WorkOrderVo> workOrderVoIPage = workOrderService.listWorkOrder(workOrderListDto);

        return Result.success(workOrderVoIPage);
    }

    /**
     * 更新工单，可更新的有工单的名称，目标组织，预期结束时间，状态，用户自定义字段
     * @param token
     * @param workOrderUpdateDto
     * @return
     */
    @Operation(summary = "工单更新")
    @PostMapping("/update")
    public Result updateWorkOrder(@RequestHeader String token, @RequestBody @Validated WorkOrderUpdateDto workOrderUpdateDto) {

        Boolean res = workOrderService.updateWorkOrder(workOrderUpdateDto);
        if (res) {
            return Result.success();
        }
        return Result.error(MessageConstant.PARAMETER_ERROR);
    }

    @Operation(summary = "工单重诉")
    @PostMapping("/repeat")
    public Result repeatWorkOrder(@RequestHeader String token,@RequestBody @Validated WorkOrderRepeatDto workOrderRepeatDto) {

        Boolean res = workOrderService.repeatWorkOrder(workOrderRepeatDto);
        if (!res){
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }

    /**
     * 从数据库删除工单，只有管理员可以操作，一般的用户只能把自的工单状态更新为关闭
     * @param token
     * @param uuids
     * @return
     */
    @Operation(summary = "总部商务删除工单")
    @PostMapping("/delete")
    public Result deleteWorkOrder(@RequestHeader String token,@RequestBody List<String> uuids) {
        if (uuids==null||uuids.isEmpty()) {
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        Boolean res = workOrderService.deleteWorkOrder(uuids);
        if (!res){
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }


}
